package com.ruoyi.app

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.ruoyi.app.adapter.FragmentPagerAdapter
import com.ruoyi.app.adapter.NavigationAdapter
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.databinding.ActivityMainBinding
import com.ruoyi.app.fragment.HomeFragment
import com.ruoyi.app.fragment.MineFragment
import com.ruoyi.app.fragment.WorkFragment
import com.ruoyi.code.Frame
import com.ruoyi.code.base.BaseBindingActivity
import com.xuexiang.xupdate.XUpdate


class MainActivity : BaseBindingActivity<ActivityMainBinding>(),
    NavigationAdapter.OnNavigationListener {

    companion object {

        private const val INTENT_KEY_IN_FRAGMENT_INDEX: String = "fragmentIndex"
        private const val INTENT_KEY_IN_FRAGMENT_CLASS: String = "fragmentClass"

        @JvmOverloads
        fun startActivity(
            context: Context,
            fragmentClass: Class<out Fragment>? = HomeFragment::class.java
        ) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(INTENT_KEY_IN_FRAGMENT_CLASS, fragmentClass)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    private var navigationAdapter: NavigationAdapter? = null
    private var pagerAdapter: FragmentPagerAdapter<Fragment>? = null

    private val list = ArrayList<NavigationAdapter.MenuItem>().apply {
        add(
            NavigationAdapter.MenuItem(
                Frame.getString(R.string.index_nav_index),
                Frame.getDrawable(R.drawable.index_selector)
            )
        )
        add(
            NavigationAdapter.MenuItem(
                Frame.getString(R.string.work_nav_index),
                Frame.getDrawable(R.drawable.work_selector)
            )
        )
        add(
            NavigationAdapter.MenuItem(
                Frame.getString(R.string.mine_nav_index),
                Frame.getDrawable(R.drawable.mine_selector)
            )
        )
    }

    override fun initView() {

        navigationAdapter = NavigationAdapter(list).apply {
            setOnNavigationListener(this@MainActivity)
            binding.rvHomeNavigation.adapter = this
        }

        pagerAdapter = FragmentPagerAdapter<Fragment>(this).apply {
            addFragment(HomeFragment.newInstance())
            addFragment(WorkFragment.newInstance())
            addFragment(MineFragment.newInstance())
            binding.vpHomePager.adapter = this
        }

        onNewIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        pagerAdapter?.let {
            switchFragment(
                it.getFragmentIndex(
                    getIntent().getSerializableExtra(
                        INTENT_KEY_IN_FRAGMENT_CLASS
                    ) as Class<out Fragment>
                )
            )
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.vpHomePager.let {
            // 保存当前 Fragment 索引位置
            outState.putInt(INTENT_KEY_IN_FRAGMENT_INDEX, it.currentItem)
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        // 恢复当前 Fragment 索引位置
        switchFragment(savedInstanceState.getInt(INTENT_KEY_IN_FRAGMENT_INDEX))
    }

    override fun initData() {
        XUpdate.newBuild(this)
            .promptThemeColor(ContextCompat.getColor(this, R.color.red))
            .updateUrl(ConfigApi.uploadApp)
            .update()
    }

    private fun switchFragment(fragmentIndex: Int) {
        if (fragmentIndex < 0) {
            return
        }
        binding.vpHomePager.currentItem = fragmentIndex
        navigationAdapter?.setSelectedPosition(fragmentIndex)
    }

    override fun onNavigationItemSelected(position: Int): Boolean {
        binding.vpHomePager.currentItem = position
        return true
    }

}